﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Swift;
using UniLua;

public class MonsterMgr : MonoBehaviour {

    public Transform PlayerPos;
    public Transform EnemyPos;
    public Monster Mon;

    public Transform MonsterSignRoot;
    public MonsterSign MonSign;

    private void Awake()
    {
        CombatMgr.monMgr = this;
    }

    // 添加怪物
    Dictionary<LuaMonster, Monster> mon2m = new Dictionary<LuaMonster, Monster>();
    public void MonsterAdded(LuaMonster m)
    {
        var mon = Instantiate(Mon) as Monster;
        mon.hMonster = m;
        mon.gameObject.SetActive(true);
        mon.transform.SetParent(m.IsPlayer ? PlayerPos : EnemyPos);
        mon.transform.localPosition = m.Pos == 1 ? Vector3.zero : 
            (m.Pos == 2 ? Vector3.left * 20 : Vector3.right * 20);

        var ms = Instantiate(MonSign) as MonsterSign;
        ms.gameObject.SetActive(true);
        ms.transform.SetParent(MonsterSignRoot);
        ms.transform.localPosition = Vector3.zero;
        ms.transform.localScale = Vector3.one;
        ms.SetMonsterInfo(m);
        UIMgr.SetUIByWorldPos(ms.GetComponent<RectTransform>(), mon.transform.position);
        mon2Sign[m] = ms;
        mon2m[m] = mon;
    }

    // 移除怪物
    public void MonsterRemoved(LuaMonster m)
    {
        Destroy(mon2m[m].gameObject);
        Destroy(mon2Sign[m].gameObject);

        mon2m.Remove(m);
        mon2Sign.Remove(m);
    }

    // 怪物属性改变
    Dictionary<LuaMonster, MonsterSign> mon2Sign = new Dictionary<LuaMonster, MonsterSign>();
    public void AttrChanged(LuaMonster m, string attr)
    {
        mon2Sign[m].Refresh();
    }

    public void BuffsChanged(LuaMonster m)
    {
        mon2Sign[m].Refresh();
    }

    public void Clear()
    {
        foreach (var m in mon2m.Values)
            Destroy(m.gameObject);

        mon2m.Clear();

        foreach (var ms in mon2Sign.Values)
            Destroy(ms.gameObject);

        mon2Sign.Clear();
    }
}
